package cz.acies.genea.utils;

import java.util.Arrays;
import java.util.List;

import cz.acies.utils.DateUtils;
import cz.acies.utils.StringUtils;

public class GedUtils
{
	private static List<String> months = Arrays.asList(new String[] { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" });
	
	public static void main(String[] args) {
		try {
			/*
			ListOrderedMap map = new ListOrderedMap();
			
			map.put("A", "AAA");
			map.put("C", "CCC");
			map.put("F", "FFF");
			
			System.out.println(map);
			
			map.put(1, "B", "BBB");
			
			System.out.println(map);

			map.put("G", "GGG");
			
			System.out.println(map);
			*/
			
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public static java.util.Date parseDate(String str) throws Exception {
		java.util.Date date = java.sql.Date.valueOf("2099-01-01");
		if (str != null) {
			String[] as = str.split(" ");
			String ds = "";
			if (as.length >= 3) {
				String d = as[0];
				String m = as[1];
				String y = as[2];
				int mm = months.indexOf(m);
				//System.out.println("m: " + m + ", mm" + mm);
				if (mm < 0 || mm > 11) { 
					y = "2099"; mm = 0; d = "1"; 
				}
				ds = y + "-" + StringUtils.leftPad(""+(mm+1), 2, "0")+"-"+ StringUtils.leftPad(d, 2, "0");
				date = DateUtils.parseDate(ds, "yyyy-MM-dd");
			} else if (as.length == 2) {
				String d = "1";
				String m = as[0];
				String y = as[1];
				int mm = months.indexOf(m);
				if (mm < 0 || mm > 11) { 
					y = "2099"; mm = 0; d = "1"; 
				}
				ds = y + "-" + StringUtils.leftPad(""+(mm+1), 2, "0")+"-"+ StringUtils.leftPad(d, 2, "0");
				date = DateUtils.parseDate(ds, "yyyy-MM-dd");
			}
			if (as.length == 1) {
				String d = "1";
				String y = as[0];
				int mm = 1;
				ds = y + "-" + StringUtils.leftPad(""+(mm+1), 2, "0")+"-"+ StringUtils.leftPad(d, 2, "0");
				date = DateUtils.parseDate(ds, "yyyy-MM-dd");
			}
		}
		return date;
	}

}
